package com.health.mapp.impl;

import java.math.BigDecimal;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.health.db.dto.StuffDetailDto;
import com.health.db.model.GoodsDataStuffRemark;
import com.health.db.model.GoodsDataUser;
import com.health.db.model.GoodsPriceList;
import com.health.db.model.GoodsPriceListTemp;
import com.health.db.model.UserAccountLedger;
import com.health.db.model.UserLIST;
import com.health.db.service.sm.IGoodsDataService;
import com.health.db.service.sm.IRongService;
import com.health.db.service.sm.ISMSSendService;
import com.health.db.service.user.IGoodsPriceListService;
import com.health.db.service.user.IStuffListService;
import com.health.db.service.user.IUserAccountLedgerService;
import com.health.db.service.user.IUserLISTService;
import com.health.mapp.bean.Header;
import com.health.mapp.bean.ReceiveHealthRecordReq;
import com.health.mapp.bean.ReceiveHealthRecordResp;
import com.health.mapp.utils.Constant;
import com.health.mapp.utils.DateUtils;
import com.health.mapp.utils.Response;
import com.health.mapp.utils.ResultCode;

/**
 *健康档案接口
 * 
 */
public class ReceiveHealthRecordImpl implements ActionInterface {

	// service import
	private IGoodsPriceListService goodsPriceListService;

	private IStuffListService stuffListService;

	private IGoodsDataService goodsDataService;

	private IUserAccountLedgerService userAccountLedgerService;
	
	private IRongService rongService  ; 
	
	private ISMSSendService sMSSendService;
	
	private IUserLISTService userLISTService;

	@Override
	public Response execute(Header header, Object reqBean) throws Exception {

		ReceiveHealthRecordReq req = (ReceiveHealthRecordReq) reqBean;
		ReceiveHealthRecordResp _resp = new ReceiveHealthRecordResp();
		String docId = req.getDocId();
		// type不再需要，此类只做医生对订单进行接受操作
		String type = req.getType();
		String recId = req.getRecId();

		if ( StringUtils.isBlank(type)
				|| StringUtils.isBlank(recId)) {
			_resp.setErrorCode(ResultCode.OPERATION_LAKE_PARAMETER_ERROR);
			_resp.setErrorMsg(ResultCode.OPERATION_LAKE_PARAMETER_ERROR_NOTE);
			return _resp;
		} else {

			// 获取当前要处理的订单
			GoodsDataUser dataUser = goodsDataService.selectGoodsDataUser(Long.valueOf(recId));
			GoodsDataUser dataUserUpdate = new GoodsDataUser();
			dataUserUpdate.setRecid(dataUser.getRecid());

			// 判断订单状态
			if (dataUser == null) {
				_resp.setErrorCode(ResultCode.OPERATION_ORDER_ERROR_3001);
				_resp.setErrorMsg(ResultCode.OPERATION_ORDER_ERROR_3001_NOTE);
				return _resp;
			}
			
			//查询状态
			int runStatusId = dataUser.getOperFlag();

			dataUserUpdate.setUpdateDate(DateUtils.getDatetime());
			
			//医生接单/抢单
			if (type.equals("3")) {
				
				//查看医生详情
				StuffDetailDto stuffDetailDto = stuffListService.selectByStuffId(Long.valueOf(docId));
				
				if (null != stuffDetailDto
						&& stuffDetailDto.getRegisterWhere()
								.contains(String.valueOf(Constant.CONSTANTS_ORDER_CHANNEL_ID_9.intValue()))) {
				} else {
					_resp.setErrorCode(ResultCode.OPERATION_ORDER_ERROR_3001);
					_resp.setErrorMsg("本订单入参医生无权接受");
					return _resp;
				}

				if (runStatusId != Constant.CONSTANTS_HEALTH_RECORD_RUNSTATUS_ID_2) {
					if (runStatusId == Constant.CONSTANTS_HEALTH_RECORD_RUNSTATUS_ID_3.intValue()
							&& !(Integer.valueOf(docId).intValue() == dataUser.getAccepterId().intValue())) {
						_resp.setErrorCode(ResultCode.OPERATION_ORDER_ERROR_3013);
						_resp.setErrorMsg(ResultCode.OPERATION_ORDER_ERROR_3013_NOTE);
						return _resp;
					} else {
						_resp.setErrorCode(ResultCode.OPERATION_ORDER_ERROR_3014);
						_resp.setErrorMsg(ResultCode.OPERATION_ORDER_ERROR_3014_NOTE);
						return _resp;
					}
				}
				GoodsPriceList example = new GoodsPriceList();
				example.setSerialIdno(req.getDocId().toString());
				example.setGoodsid(9);
				// 查询医生的单体价格
				GoodsPriceListTemp goodsPriceList = goodsPriceListService.selectByGoodsIdDocId(example);

				if (null == goodsPriceList) {
					_resp.setErrorCode(ResultCode.OPERATION_ORDER_ERROR_3001);
					_resp.setErrorMsg("本订单入参医生无权接受");
					return _resp;
				}
				
				//接单
				dataUserUpdate.setAccepterId(Long.valueOf(docId));
				dataUserUpdate.setAccepterName(stuffDetailDto.getStuffName());
				dataUserUpdate.setRegisterPayoffTotal(new BigDecimal(goodsPriceList.getPrice00()));
				dataUserUpdate.setOperFlag((byte) Constant.CONSTANTS_HEALTH_RECORD_RUNSTATUS_ID_3.intValue());

				int returnFlag=0;
				returnFlag =goodsDataService.updateGoodsDataUserAccept(dataUserUpdate);
				
				if (returnFlag==1) {
					
				}else if(returnFlag==-2){
					_resp.setErrorCode(ResultCode.OPERATION_ORDER_ERROR_3013);
					_resp.setErrorMsg(ResultCode.OPERATION_ORDER_ERROR_3013_NOTE);
					return _resp;
				}else {
					_resp.setErrorCode(ResultCode.OPERATION_ORDER_ERROR_3013);
					_resp.setErrorMsg(ResultCode.OPERATION_ORDER_ERROR_3013_NOTE);
					return _resp;
				}
				
			//医生提交发布	
			} else if (type.equals("4")) {
				
				//验证医生输入项
				List<GoodsDataStuffRemark> docRemarkList = dataUser.getDocRemarkList();
				
				for (int i = 0; i < docRemarkList.size(); i++) {
					GoodsDataStuffRemark remarkTemp = docRemarkList.get(i);
					if (StringUtils.isBlank(remarkTemp.getSubTitle())||StringUtils.isBlank(remarkTemp.getSubDesc())) {
						_resp.setErrorCode(ResultCode.OPERATION_ORDER_ERROR_3024);
						_resp.setErrorMsg(ResultCode.OPERATION_ORDER_ERROR_3024_NOTE);
						return _resp;
					}
				}
				

				if (runStatusId != Constant.CONSTANTS_HEALTH_RECORD_RUNSTATUS_ID_3.intValue()) {
					
					_resp.setErrorCode(ResultCode.OPERATION_ORDER_ERROR_3003);
					_resp.setErrorMsg(ResultCode.OPERATION_ORDER_ERROR_3003_NOTE);
					return _resp;
				}
				if (null == dataUser.getAccepterId()) {
					_resp.setErrorCode(ResultCode.OPERATION_ORDER_ERROR_3021);
					_resp.setErrorMsg(ResultCode.OPERATION_ORDER_ERROR_3021_NOTE);
					return _resp;
				}

				dataUserUpdate.setOperFlag((byte) Constant.CONSTANTS_HEALTH_RECORD_RUNSTATUS_ID_4.intValue());
				goodsDataService.updateGoodsDataUser(dataUserUpdate, null);
			//客服发布 医生可接
			} else if (type.equals("2")) {

				if (null == dataUser.getPatient()) {
					_resp.setErrorCode(ResultCode.OPERATION_ORDER_ERROR_3020);
					_resp.setErrorMsg(ResultCode.OPERATION_ORDER_ERROR_3020_NOTE);
					return _resp;
				}
				dataUserUpdate.setOperFlag((byte) Constant.CONSTANTS_HEALTH_RECORD_RUNSTATUS_ID_2.intValue());
				goodsDataService.updateGoodsDataUser(dataUserUpdate, null);
				rongService.PublishSystemRecordToDocMsg(dataUser);
			//客户发布 用户可看	
			} else if (type.equals("5")) {

				if (runStatusId != Constant.CONSTANTS_HEALTH_RECORD_RUNSTATUS_ID_4) {
					_resp.setErrorCode(ResultCode.OPERATION_ORDER_ERROR_3003);
					_resp
							.setErrorMsg(ResultCode.OPERATION_ORDER_ERROR_3003_NOTE);
					return _resp;
				}
				// 入账
				dataUserUpdate.setOperFlag((byte) Constant.CONSTANTS_HEALTH_RECORD_RUNSTATUS_ID_5.intValue());

				UserAccountLedger accountLedge = userAccountLedgerService
						.selectByUserId(Long.valueOf(docId));
				// 医生账户表

				int accountType = 0;
				// 如果医生应得到的为0 不做插入日志处理
				if (dataUser.getRegisterPayoffTotal().compareTo(new BigDecimal(0)) > 0) {

					if (null != accountLedge&& accountLedge.getAcctpageid().intValue() != 0) {
						accountType = 1;
						// 医生获得的补偿累加
						accountLedge.setBalanceTotal
							(accountLedge.getBalanceTotal().add(dataUser.getRegisterPayoffTotal()));
					} else {

						accountLedge = new UserAccountLedger();
						accountLedge.setBalanceTotal(dataUser.getRegisterPayoffTotal());
						accountLedge.setAccountid(Long.valueOf(docId));
						accountLedge.setIsUsedFlag((short) 1);
						accountLedge.setColumnid(1);
					}
					// 更新订单表，更新账户表
					goodsDataService.updateGoodsDataUserFinish(dataUserUpdate,accountLedge, accountType,dataUser);
				} else {
					goodsDataService.updateGoodsDataUser(dataUserUpdate, null);
				}

				rongService.PublishSystemMsg(String.valueOf(dataUser.getAccepterId().intValue()),String.valueOf(dataUser.getOwnerId().intValue()),
						Constant.CONSTANTS_RONGYUN_H_ORDER_5);
				
				 UserLIST userLIST = userLISTService.selectByUserId(dataUser.getOwnerId());
				if (null!=userLIST&&userLIST.getUserNo()!=null) {
					sMSSendService.notice85735036(userLIST.getUserNo());
				}
				
			} else if (type.equals("6")) {

				dataUserUpdate.setOperFlag((byte) Constant.CONSTANTS_HEALTH_RECORD_RUNSTATUS_ID_6.intValue());
				goodsDataService.updateGoodsDataUser(dataUserUpdate, null);
				
			//打回重做
			} else if (type.equals("7")) {

				dataUserUpdate.setOperFlag((byte) Constant.CONSTANTS_HEALTH_RECORD_RUNSTATUS_ID_3.intValue());
				goodsDataService.updateGoodsDataUser(dataUserUpdate, null);
				rongService.PublishSystemMsg(docId,String.valueOf(dataUser.getAccepterId().intValue()),Constant.CONSTANTS_RONGYUN_H_ORDER_7);
				UserLIST userLIST = userLISTService.selectByUserId(dataUser.getAccepterId());
				if (null!=userLIST&&userLIST.getUserNo()!=null) {
					sMSSendService.notice85765027(userLIST.getUserName(),  userLIST.getUserNo().replace("#", ""));
				}
				
			} else if (type.equals("8")) {

				//暂不提供 再次发布
				//dataUserUpdate.setOperFlag((byte) Constant.CONSTANTS_HEALTH_RECORD_RUNSTATUS_ID_2.intValue());
				//goodsDataService.updateGoodsDataUser(dataUserUpdate, null);
			} 
			
			else {

				_resp.setErrorCode(ResultCode.OPERATION_ORDER_ERROR_3004);
				_resp.setErrorMsg(ResultCode.OPERATION_ORDER_ERROR_3004_NOTE);
				return _resp;
			}
		}

		_resp.setErrorCode(ResultCode.OPERATION_SUCCESS);
		_resp.setErrorMsg(ResultCode.OPERATION_SUCCESS_NOTE);

		return _resp;
	}

	public IGoodsPriceListService getGoodsPriceListService() {
		return goodsPriceListService;
	}

	public void setGoodsPriceListService(
			IGoodsPriceListService goodsPriceListService) {
		this.goodsPriceListService = goodsPriceListService;
	}

	public IStuffListService getStuffListService() {
		return stuffListService;
	}

	public void setStuffListService(IStuffListService stuffListService) {
		this.stuffListService = stuffListService;
	}

	public IGoodsDataService getGoodsDataService() {
		return goodsDataService;
	}

	public void setGoodsDataService(IGoodsDataService goodsDataService) {
		this.goodsDataService = goodsDataService;
	}

	public IUserAccountLedgerService getUserAccountLedgerService() {
		return userAccountLedgerService;
	}

	public void setUserAccountLedgerService(
			IUserAccountLedgerService userAccountLedgerService) {
		this.userAccountLedgerService = userAccountLedgerService;
	}

	public IRongService getRongService() {
		return rongService;
	}

	public void setRongService(IRongService rongService) {
		this.rongService = rongService;
	}

	public ISMSSendService getsMSSendService() {
		return sMSSendService;
	}

	public void setsMSSendService(ISMSSendService sMSSendService) {
		this.sMSSendService = sMSSendService;
	}

	public IUserLISTService getUserLISTService() {
		return userLISTService;
	}

	public void setUserLISTService(IUserLISTService userLISTService) {
		this.userLISTService = userLISTService;
	}
	
	
	
}
